<template>
  <div class="login-container">
    <!-- 登录框 -->
    <div class="login-box">
      <!-- 头像 -->
      <div class="avater">
        <img src="../../assets/imgs/avater.png" alt="">
      </div>
      <!-- 表单区域 -->
      <div class="form-box">
        <el-form :model="user" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="password">
            <el-input type="password" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="verifyCode">
            <div class="verifyCode_box">
              <el-input v-model="user.verifyCode" placeholder="请输入手机验证码" prefix-icon="el-icon-mobile"
                        class="verifyCode"></el-input>
              <img src="../../assets/imgs/yzm.png" alt="" class="verifyCode_img">
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {request} from "../../request/index";

export default {
  data() {
    return {
      user: {
        username: 'admin',
        password: '123456',
        verifyCode: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名称', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //登录之后跳转主页面
          let result = await request({
            url: `/user/login/${this.user.username}/${this.user.password}`,
            method: 'post',
          })
          console.log(result);
          if (result.code === 200) {
            this.$message({
              showClose: true,
              message: '恭喜你，登陆成功',
              type: 'success'
            });
            //还需要将token存到本地存储中
            window.localStorage.setItem("JWT_TOKEN", result.data.token);
            //还需要获取用户信息
            this.getUserInfo();
          } else {
            var message = result.message;
            this.$message({
              showClose: true,
              message: message,
              type: 'error'
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    async getUserInfo() {
      let result = await request({
        url: '/user/info/',
        method: 'get',
      })
      if (result.code === 200) {
        //将用户信息存入vuex
        this.$store.commit("setUserInfo", result.data.userInfoVO)
        this.$router.push("/home");
      } else {
        this.$message.error({
          title: "登入失败",
          message: result.message,
          type: "error"
        });
      }
    }
  },
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background-color: #FFE4E1;

  .login-box {
    height: 350px;
    width: 600px;
    background-color: #FFFFFF;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avater {
      width: 130px;
      height: 130px;
      background-color: #fff;
      border-radius: 50%;
      padding: 10px;
      margin: -65px auto;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eeeeee;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      }
    }

    .form-box {
      padding: 30px;
      margin-top: 50px;

      .verifyCode_box {
        display: flex;

        .el-input {
          flex: 2;
        }

        img {
          flex: 1;
        }
      }
    }
  }
}
</style>